package com.ruoyi.web.controller.bkcontroller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Expenditure;
import com.ruoyi.system.service.IExpenditureService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 支出Controller
 * 
 * @author ruoyi
 * @date 2021-08-10
 */
@RestController
@RequestMapping("/system/expenditure")
public class ExpenditureController extends BaseController
{
    @Autowired
    private IExpenditureService expenditureService;

    /**
     * 查询支出列表
     */
    @PreAuthorize("@ss.hasPermi('system:expenditure:list')")
    @GetMapping("/list")
    public TableDataInfo list(Expenditure expenditure)
    {
        startPage();
        List<Expenditure> list = expenditureService.selectExpenditureList(expenditure);
        return getDataTable(list);
    }

    /**
     * 导出支出列表
     */
    @PreAuthorize("@ss.hasPermi('system:expenditure:export')")
    @Log(title = "支出", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Expenditure expenditure)
    {
        List<Expenditure> list = expenditureService.selectExpenditureList(expenditure);
        ExcelUtil<Expenditure> util = new ExcelUtil<Expenditure>(Expenditure.class);
        return util.exportExcel(list, "支出数据");
    }

    /**
     * 获取支出详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:expenditure:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(expenditureService.selectExpenditureById(id));
    }

    /**
     * 新增支出
     */
    @PreAuthorize("@ss.hasPermi('system:expenditure:add')")
    @Log(title = "支出", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Expenditure expenditure)
    {
        expenditure.setCreateBy(getUsername());
        expenditure.setReimbursementerId(getUserId());
        return toAjax(expenditureService.insertExpenditure(expenditure));
    }

    /**
     * 修改支出
     */
    @PreAuthorize("@ss.hasPermi('system:expenditure:edit')")
    @Log(title = "支出", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Expenditure expenditure)
    {
        expenditure.setUpdateBy(getUsername());
        return toAjax(expenditureService.updateExpenditure(expenditure));
    }

    /**
     * 删除支出
     */
    @PreAuthorize("@ss.hasPermi('system:expenditure:remove')")
    @Log(title = "支出", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(expenditureService.deleteExpenditureByIds(ids));
    }

    /**
     * 审核支出
     */
    @PreAuthorize("@ss.hasPermi('system:expenditure:audite')")
    @Log(title = "审核支出", businessType = BusinessType.AUDITE)
    @PutMapping("/audite")
    public AjaxResult audite(@RequestBody Expenditure expenditure)
    {
        expenditure.setAuditerId(getUserId());
        expenditure.setUpdateBy(getUsername());
        return toAjax(expenditureService.auditeExpenditure(expenditure));
    }
}
